/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik.lego.controller;

import java.util.List;

import lejos.nxt.Button;
import lejos.nxt.LCD;
import ch.bfh.ti.kybernetik.lego.ki.RobKIFactr;
import ch.bfh.ti.kybernetik.lego.ki.RoboterKI;

/**
 * {@link RoboterKI} is the Lego Roboter Controller Class <br/>
 * <br/>
 * <strong>Note:</strong>We had to make sure that the Class names don't exceed
 * the Lego JVM limit of 20 characters
 * 
 */
public class RobKIContr {

	private static final int MAX_LCD_LENGTH = 16;

	private final List<RoboterKI> roboterKIList;

	private RoboterKI currentSelectedRoboterKI;

	private SensorActorController sensorActorController;

	public RobKIContr(List<RoboterKI> roboterKIList) {
		this.roboterKIList = roboterKIList;
		currentSelectedRoboterKI = roboterKIList.get(0);
		sensorActorController = SensorActorController.getInstance();
	}

	public void run() {
		boolean buttonLeftPressed = false;
		boolean buttonRightPressed = false;

		int mode = -1;

		display("Press Arrow to", 0, 2);
		display("toggle Mode", 0, 3);

		while (true) {
			if (Button.LEFT.isPressed() && !buttonLeftPressed) {
				buttonLeftPressed = true;
				if (--mode == -2) {
					mode = roboterKIList.size() - 1;
				}
			}
			if (!Button.LEFT.isPressed()) {
				buttonLeftPressed = false;
			}

			if (Button.RIGHT.isPressed() && !buttonRightPressed) {
				buttonRightPressed = true;
				if (++mode == roboterKIList.size()) {
					mode = -1;
				}
			}
			if (!Button.RIGHT.isPressed()) {
				buttonRightPressed = false;
			}
			if (mode == -1) {
				display("Aus", 0, 5);
				currentSelectedRoboterKI.stopMotor();
			} else {
				currentSelectedRoboterKI = roboterKIList.get(mode);
				String roboterKIName = currentSelectedRoboterKI.getRoboterKIName();

				roboterKIName = fillChar(roboterKIName);

				display(roboterKIName, 0, 5);
				currentSelectedRoboterKI.calculateNewRoboterMotorSpeed(sensorActorController.getLightSensorLeftValue(),
						sensorActorController.getLightSensorRightValue());
			}

			sensorActorController.setMotorLeftSpeed(currentSelectedRoboterKI.getMotorSpeedLeft());
			sensorActorController.setMotorRightSpeed(currentSelectedRoboterKI.getMotorSpeedRight());

			display("Left: " + currentSelectedRoboterKI.getMotorSpeedLeft(), 0, 0);
			display("Right: " + currentSelectedRoboterKI.getMotorSpeedRight(), 0, 1);

			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
			}
		}
	}

	private void display(String str, int x, int y) {
		String roboterKIName = fillChar(str);
		LCD.drawString(roboterKIName, x, y);
	}

	private String fillChar(String roboterKIName) {
		int diff = MAX_LCD_LENGTH - roboterKIName.length();
		for (int i = 0; i < diff; i++) {
			roboterKIName = roboterKIName + " ";
		}
		return roboterKIName;
	}

	/**
	 * Main Method for the Lego Mindstorm JVM
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		List<RoboterKI> list = RobKIFactr.getAvailableRoboterKIList();
		
		RobKIContr controller = new RobKIContr(list);
		controller.run();
	}
}
